package com.lz.football.module.compete.item
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.compete.vo.InlandRankVo;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.compete.SingleChaglleView;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.ImageContain;

	/**
	 *国王三大将 勇士item 
	 * @author Administrator
	 * 
	 */	
	public class RankItem extends Sprite implements IComponent
	{
		/**
		 * 
		 */
		private var _skin:MovieClip;
		/**
		 *名称  
		 */
		private var playerName:TextField;
		/**
		 *图标容器 
		 */
		private var icon:MovieClip;
		private var _callBack:Function;
		private var _vo:InlandRankVo;
		
		/**
		 *是否需要再次绽放队徽图标  
		 */
		private var _isScanel:Boolean;
		/**
		 * 
		 */
		private var _str:String;
		
		public function RankItem()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_isScanel = false;
			_str = "";
			switch(view as int )
			{
				case 1:
					_skin = Reflection.createInstance("com.lz.modul.compete.kingItem");
					_skin.mouseChildren = false;
					_skin.mouseEnabled = false;
					_str = "国王";
					break;
				case 2:
				case 3:
				case 4:
					_skin = Reflection.createInstance("com.lz.modul.compete.seniorItem");
					_str = "大将";
					break;
				default:
					_skin = Reflection.createInstance("com.lz.modul.compete.warrior");
					_isScanel = true;
					break;
			}
			if(_skin == null)return;
			this.addChild(_skin);
			playerName = _skin.playerName;
			playerName.mouseEnabled = false;
			icon = _skin.icon;
			this.buttonMode = true;
			addEvtListener();
		}
		/**
		 *设置数据  
		 * 
		 */
		public function setData($vo:InlandRankVo):void
		{
			
			
			initUI($vo.sort);
			_vo = $vo;
//			trace("队徽ID："+$vo.logo);
			if(icon != null)
			{
				if(AppData.getInstance().clubDatamanager.getTeamIconVo($vo.logo) != null)
				{
					var lo:MovieClip = Reflection.createInstance(AppData.getInstance().clubDatamanager.getTeamIconVo($vo.logo).iconurl);
					if(lo != null)
					{
						
						if(_isScanel)
							lo.scaleX = lo.scaleY = 0.2;
						else
							lo.scaleX = lo.scaleY = 0.4;
						
						lo.x = -lo.width >> 1;
						lo.y = -lo.height >> 1;
						icon.addChild(lo);
						
					}
				}else
				{
					var iC:ImageContain = new ImageContain();
					iC.setSize(30,30);
					iC.url = GlobalDef.Web_Res_Url + "clubicon/30-30/" + $vo.logo + ".png";
					iC.x = -iC.width >> 1;
					iC.y = -iC.height >> 1;
					icon.addChild(iC);
				}
			}
			playerName.text = _str + ""+$vo.clubname;
		}
		/**
		 *重置 
		 * 
		 */
		public function reset():void
		{
			if(icon != null)
			{
				DisplayObjectUtil.removeAllChildren(icon);
			}
			DisplayObjectUtil.removeAllChildren(_skin);
//			icon = null;
//			_skin = null;
			_vo = null;
		}
		
		public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		protected function onClickHandler(event:MouseEvent):void
		{
			if(_vo.clubid < 20000)//小于20000是NPC数据取第11个数据
			{
				AppData.getInstance().competeDataManager.selectchallengecost(11);
			}else
			{
				AppData.getInstance().competeDataManager.selectchallengecost(_vo.sort);
			}
			AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.OTHER_INFO,{id:_vo.clubid}));
		}
//		private function isChangell($type:int):void
//		{
//			if($type == Alert.YES)
//			{
//				_callBack(_vo.clubid);
//			}
//		}
		public function removeEvtListener():void
		{
			this.removeEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			_callBack = null;
			reset();
		}

		/**
		 *回调 
		 */
		public function get callBack():Function
		{
			return _callBack;
		}

		/**
		 * @private
		 */
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}

	}
}